﻿// <copyright file="OrificeValidator.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>OrificeValidator class</summary>

namespace BESASM.EMGAATS.BusinessClasses
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using FluentValidation;

  /// <summary>
  /// A class for validating orifice instances
  /// </summary>
  public class OrificeValidator : AbstractValidator<IOrifice>
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="OrificeValidator"/> class
    /// </summary>
    /// <param name="ruleSet">The set of rules to apply</param>
    public OrificeValidator(RuleSet ruleSet)
    {
      switch (ruleSet)
      {
        case BusinessClasses.RuleSet.MasterDataCommit:
          this.OrificeAreaGreaterThanZero(ErrorLevel.Error);
          this.OrificeCoefficientGreaterThanZero(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.ModelDataCommit:
          this.OrificeAreaGreaterThanZero(ErrorLevel.Error);
          this.OrificeCoefficientGreaterThanZero(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.ModelDeployment:
          this.OrificeAreaGreaterThanZero(ErrorLevel.Error);
          this.OrificeCoefficientGreaterThanZero(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.NetworkTracing:
          break;
        case BusinessClasses.RuleSet.PropertyDialog:
          this.OrificeAreaGreaterThanZero(ErrorLevel.Error);
          this.OrificeCoefficientGreaterThanZero(ErrorLevel.Error);
          break;
      }
    }

    private void OrificeAreaGreaterThanZero(ErrorLevel level)
    {
      RuleFor(orifice => orifice.AreaSqFt)
        .GreaterThan(0.0)
        .WithState(orifice => new ValidationState(orifice, level))
        .WithMessage(
          "Orifice ({0}-{1}) has an area equal to or less than zero.",
          orifice => orifice.USNode,
          orifice => orifice.DSNode);
    }

    private void OrificeCoefficientGreaterThanZero(ErrorLevel level)
    {
      RuleFor(orifice => orifice.Coefficient)
        .GreaterThan(0.0)
        .WithState(orifice => new ValidationState(orifice, level))
        .WithMessage(
          "Orifice ({0}-{1}) has a coefficient equal to or less than zero.",
          orifice => orifice.USNode,
          orifice => orifice.DSNode);
    }
  }
}
